package com.icode.views;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.icode.views.utils.AbstractApplet;

public class ClientApp extends AbstractApplet implements ActionListener {
	private JButton btnSearch, btnMap, btnReset;
	private JLabel lblName, lblAddrIn, lblDetail, lblAddrOut, lblBus,
			lblResult, lblRestaurant, lblHotel;
	private JTextField txtName, txtAddrIn, txtAddrOut;
	private JList listBus, listRestaurant, listHotel, listResult;
	private JTextArea txtDetail;

	private Vector<String> vtrBus, vtrRestaurant, vtrHotel, vtrResult;

	@Override
	public void createApplet() {
		// Create components
		this.btnSearch = new JButton("SEARCH");
		this.btnMap = new JButton("MAP");
		this.btnReset = new JButton("RESET");

		this.lblName = new JLabel("Name");
		this.lblAddrIn = new JLabel("Address");

		this.lblDetail = new JLabel("DETAILS");
		this.lblAddrOut = new JLabel("Address");
		this.lblBus = new JLabel("Buses");
		this.lblRestaurant = new JLabel("Restaurants");
		this.lblHotel = new JLabel("Hotels");

		this.lblResult = new JLabel("RESULTS");

		this.txtName = new JTextField(30);
		this.txtAddrIn = new JTextField(30);

		this.txtAddrOut = new JTextField(30);
		this.txtAddrOut.setEditable(false);

		this.txtDetail = new JTextArea();
		this.txtDetail.setEditable(false);

		this.vtrBus = new Vector<String>();
		this.vtrRestaurant = new Vector<String>();
		this.vtrHotel = new Vector<String>();
		this.vtrResult = new Vector<String>();

		this.listBus = new JList<>(vtrBus);
		this.listBus.setLayoutOrientation(JList.VERTICAL);
		this.listRestaurant = new JList<>(vtrRestaurant);
		this.listHotel = new JList<>(vtrHotel);
		this.listResult = new JList<>(vtrResult);

		// Create containers
		JPanel p1 = new JPanel(new BorderLayout());
		JPanel p1Mig = new JPanel(new MigLayout());
		JPanel p1Flow = new JPanel(new FlowLayout(FlowLayout.RIGHT));

		JPanel p2 = new JPanel(new MigLayout());
		JPanel p3 = new JPanel(new MigLayout());
		JScrollPane p4 = new JScrollPane(this.txtDetail);

		JSplitPane spLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false,
				p1, p3);
		spLeft.setDividerLocation(340);
		JSplitPane spRight = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false,
				p2, p4);
		spRight.setDividerLocation(450);

		JSplitPane spCenter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				false, spLeft, spRight);
		spCenter.setDividerLocation(500);

		// Add components to containers
		p1Mig.add(this.lblName);
		p1Mig.add(this.txtName, "wrap");
		p1Mig.add(this.lblAddrIn);
		p1Mig.add(this.txtAddrIn);

		p1Flow.add(this.btnSearch);
		p1Flow.add(this.btnReset);

		p1.add(p1Mig, BorderLayout.CENTER);
		p1.add(p1Flow, BorderLayout.SOUTH);

		p2.add(this.lblDetail, "wrap");
		p2.add(this.lblAddrOut, "wrap");
		p2.add(this.txtAddrOut, "wrap");
		p2.add(this.lblBus, "wrap");
		JScrollPane spBus = new JScrollPane(this.listBus);
		p2.add(spBus, "wrap");
		p2.add(this.lblRestaurant, "wrap");
		JScrollPane spRestaurant = new JScrollPane(this.listRestaurant);
		p2.add(spRestaurant, "wrap");
		p2.add(this.lblHotel, "wrap");
		JScrollPane spHotel = new JScrollPane(this.listHotel);
		p2.add(spHotel, "wrap");
		p2.add(this.btnMap);

		p3.add(this.lblResult, "wrap");
		JScrollPane spResult = new JScrollPane(this.listResult);
		p3.add(spResult);
		// Add to applet
		this.getContentPane().add(spCenter);
		// Reg Events
		this.btnSearch.addActionListener(this);
		this.btnReset.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.btnSearch) {

		}
		
		if(e.getSource() == this.btnReset){
			this.txtAddrIn.setText("");
			this.txtName.setText("");
		}
	}

	@Override
	public void init() {
		super.init();
		this.createApplet();
		this.showApplet();
	}
}
